package ciel.image;

import org.mt4j.components.TransformSpace;
import org.mt4j.components.visibleComponents.shapes.MTEllipse;
import org.mt4j.components.visibleComponents.widgets.MTImage;
import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapProcessor;
import org.mt4j.util.math.Vector3D;
import org.mt4j.util.math.Vertex;

import processing.core.PApplet;
import processing.core.PGraphics;
import processing.core.PImage;
import ciel.debugging.CielDebugger;
import ciel.menu.CielThumbnailMenu;
import ciel.res.CielSettings;
import ciel.scene.CielImageScene;

public class CielThumbnail extends MTImage{
	private boolean onCanvas = false;

	public CielThumbnail(PApplet pApplet, PImage texture) {
		super(pApplet, texture);
		setWidthXYGlobal(CielSettings.Dimensions.thumbnailWidth);
		setNoStroke(true);
		setStrokeWeight(0);
		setAnchor(PositionAnchor.CENTER);
		
		TapProcessor tp = new TapProcessor(pApplet);
		tp.setEnableDoubleTap(true);
		registerInputProcessor(tp);
	}
	
	public void changeStatus() {
		if(!onCanvas) {
			CielDebugger.log("Disabling Thumbnail");
			setVisible(false);
			onCanvas = true;
		} else {
			CielDebugger.log("Enabling Thumbnail");
			setVisible(true);
			onCanvas = false;
		}
	}
	
	@Override
	public void drawComponent(PGraphics g) {
		// TODO Auto-generated method stub
		super.drawComponent(g);
		MTEllipse parentBounds = ((MTEllipse)getParent());
		
		byte count = 0;
		Vertex[] V = getVerticesGlobal();
		for(Vertex v: V) {
			if(!parentBounds.containsPointGlobal(v)) {
				count++;
				if(count > 1){
					if((v.getX() > 0 && v.getX() < CielSettings.Dimensions.SCREEN_WIDTH) && (v.getY() > 0 && v.getY() < CielSettings.Dimensions.SCREEN_HEIGHT)) {
							if(!onCanvas) {
								((CielImageScene) getUserData("scene")).addImageToCanvas(((String)getUserData("name")), (Vector3D)getUserData("lastPosition"));		
								setPositionGlobal(((CielThumbnailMenu)getParent()).center);
							}
					} else {
						setPositionGlobal((Vector3D)getUserData("lastPosition"));
					}
				}
			}
		}
		setUserData("lastPosition", getPosition(TransformSpace.GLOBAL));
	}
}
